package netcracker;

/**
 *
 * @author Galimov, Metsker
 */
public class Dwelling {
    
    private DwellingFloor[] floors;
    
    public Dwelling(int height, int[] flatsQuantity){
        for (int i = 0; i<height; i++){
            floors[i] = new DwellingFloor(flatsQuantity[i]);
        }
    }
    
    public Dwelling(DwellingFloor[] floors){
        this.floors = new DwellingFloor[floors.length];
        System.arraycopy(floors, 0, this.floors, 0, floors.length);
    }
    
    public int getFloorsQuantity(){
        return floors.length;
    }
    
    public int getFlatsQuantity(){
        int summ = 0;
        for (DwellingFloor df:floors){
            summ += df.flatCounter();
        }
        return summ;
    }
    
    public double getDwellingSquare(){
        double square = 0;
        for (DwellingFloor df:floors){
            for (int i=0; i<df.flatCounter(); i++)
                square += df.getFlat(i).getSquare();
        }
        return square;
    }
    
    public int getRoomsQuantity(){
        int rooms = 0;
        for (DwellingFloor df:floors){
            for (int i=0; i<df.flatCounter(); i++)
                rooms += df.getFlat(i).getRooms();
        }
        return rooms;
    }
    
    public DwellingFloor[] getDwelling(){
        return floors;
    }
    
    public DwellingFloor getFloor(int num){
        return floors[num];
    }
    
    public void editFloor(int num, DwellingFloor floor){
        floors[num] = new DwellingFloor(floor.flatCounter());
        System.arraycopy(floor, 0, floors[num], 0, floor.flatCounter());
    } 
    
    public Flat getFlat(int num){
        int floorNum = getFloorFlatByNum(num)[0];
        int flatNum = getFloorFlatByNum(num)[1];
        return floors[floorNum].getFlat(flatNum);
    }
    
    public void editFlat(int num, Flat flat){
        int floorNum = getFloorFlatByNum(num)[0];
        int flatNum = getFloorFlatByNum(num)[1];
        floors[floorNum].editFlat(flatNum, flat);
    }
    
    public void addFlat(int num, Flat flat){
        int aFloor = getFloorFlatByNum(num)[0];
        int aFlat = getFloorFlatByNum(num)[1];
        floors[aFloor].addFlat(aFlat, flat);
    }
    
    public void removeFlat(int num){
        int rFloor = getFloorFlatByNum(num)[0];
        int rFlat = getFloorFlatByNum(num)[1];
        floors[rFloor].removeFlat(rFlat);
    }
    
    public Flat getBestSpace(){
        double bestSpace = 0;
        Flat tmp = null;
        for(int i = 0; i < floors.length; i++)
            if(floors[i].getBestSpace().getSquare() > bestSpace){
                tmp = floors[i].getBestSpace();
                bestSpace = tmp.getSquare();
            }
        return tmp;
    }
    
    public Flat[] getSortedFlats(){
        int count = 0;
        for(DwellingFloor df: floors){
            count += df.flatCounter();
        }
        Flat[] mas = new Flat[count];
        for(int i = 0; i < floors.length; i++)
            for(int j=0; j < floors[i].flatCounter(); j++){
                mas[count-1] = new Flat(floors[i].getFlat(j));
                count--;
            }
        Flat tmp;
        for (int i = 0; i < mas.length - 1; i++) {
            for(int j = 0; j < mas.length - 1 - i; j++)
                if(mas[j].getSquare() < mas[j + 1].getSquare()){
                    tmp = mas[j];
                    mas[j] = mas[j + 1];
                    mas[j + 1] = tmp;
                }
            
        }
        return mas;
    }
       
    private int[] getFloorFlatByNum(int num){
        int ff[] = {0,0};
        for (int i=0; i<floors.length; i++){
            if (floors[i].flatCounter()>num){
                ff[0]=i;
                ff[1]=num;
                return ff;
            }else{
                num -=floors[i].flatCounter();
            }
        }
        return ff;
    }
    
    private int getNumByFF(int floor, int flat){
        int num = 0;
        for (int i=0; i < floor; i++){
            num += floors[i].flatCounter();
        }
        num += flat;
        return num;
    }
    
    public void showDwelling(){
        for (int i=0; i < getFloorsQuantity(); i++){
            System.out.println("Этаж №" + i);
            for (int j = 0; j < getFloor(i).flatCounter(); j++){
                System.out.printf("\t№: %2d, square: %6.2f, rooms: %2d \n",
                        getNumByFF(i, j),
                        getFloor(i).getFlat(j).getSquare(),
                        getFloor(i).getFlat(j).getRooms()
                        );
            }
            System.out.println(" ");
        }
        System.out.println(" ");
    }
    
    public void showDwellingUp(){
        for (int i=getFloorsQuantity()-1; i >= 0; --i){
            System.out.println("Этаж №" + i);
            for (int j = getFloor(i).flatCounter()-1; j >= 0; --j){
                System.out.printf("\t№: %2d, square: %6.2f, rooms: %2d \n",
                        getNumByFF(i, j),
                        getFloor(i).getFlat(j).getSquare(),
                        getFloor(i).getFlat(j).getRooms()
                        );
            }
            System.out.println(" ");
        }
        System.out.println(" ");
    }
    
}
